using UnityEngine;
using UnityEditor;
using UnityEditor.TestTools.TestRunner;

public class DrawCustomWindow : EditorWindow, IHasCustomMenu
{
    [MenuItem("Tools/CustomWindow")]
    static void CustomWindow()
    {
        DrawCustomWindow window = EditorWindow.GetWindow<DrawCustomWindow>("A new window");
        window.Show();
    }

    private Object preObj;
    private Object obj;
    private Editor preview;

    private void OnGUI()
    {
        if (GUILayout.Button("Click Me"))
        {
            Debug.Log("Hi!");
        }

        obj = EditorGUILayout.ObjectField(obj, typeof(Object), false);
        if (obj && obj != preObj)
        {
            preObj = obj;
            preview = Editor.CreateEditor(obj);
        }

        if (preview && preview.HasPreviewGUI())
            preview.OnPreviewGUI(GUILayoutUtility.GetRect(400, 400), EditorStyles.label);
    }

    private void Awake()
    {
        Debug.Log("Awake");
    }

    private void OnDestroy()
    {
        Debug.Log("Destroy");
    }

    private void OnFocus()
    {
        Debug.Log("Focus");
    }

    private void OnLostFocus()
    {
        Debug.Log("LostFocus");
    }


    private bool on;

    public void AddItemsToMenu(GenericMenu menu)
    {
        menu.AddItem(new GUIContent("???"), on, userData => { on = !on; }, null);
    }
}